﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Elentok.Common.Wpf.ViewModels.Dialogs
{
  /// <summary>
  /// Defines the view model for the Input dialog
  /// </summary>
  public interface IInputDialogViewModel : IDialogViewModel
  {
    /// <summary>
    /// Gets or sets the dialog message
    /// </summary>
    string Message { get; set; }
    
    /// <summary>
    /// Gets or sets the value of the textbox
    /// </summary>
    string InputText { get; set; }

    /// <summary>
    /// Gets the command to run with the "OK" button is clicked
    /// </summary>
    ICommand OkCommand { get; }

    /// <summary>
    /// Gets or sets whether the dialog can return an empty value.
    /// </summary>
    bool AllowEmptyInput { get; set; }
  }

  /// <summary>
  /// The view model for the Input dialog
  /// </summary>
  public class InputDialogViewModel : DialogViewModelBase, IInputDialogViewModel
  {
    #region Private Fields
    private string message;
    private string inputText;
    private bool allowEmptyInput = false;
    #endregion

    #region Ctor
    /// <summary>
    /// Initializes a new instance of the <see cref="InputDialogViewModel"/> class
    /// </summary>
    public InputDialogViewModel()
    {
      this.InputText = string.Empty;
      this.OkCommand = new ListeningCommand<object>(
        this.ExecuteOkCommand,
        this.CanExecuteOkCommand,
        this,
        "AllowEmptyInput", "InputText");
    }
    #endregion

    #region IInputDialogViewModel Members

    /// <summary>
    /// Gets or sets the dialog message
    /// </summary>
    public string Message
    {
      get
      {
        return this.message;
      }
      set
      {
        if (this.message == value)
          return;

        this.message = value;
        OnPropertyChanged("Message");
      }
    }

    /// <summary>
    /// Gets or sets the value of the textbox
    /// </summary>
    public string InputText
    {
      get
      {
        return this.inputText;
      }
      set
      {
        if (this.inputText == value)
          return;

        this.inputText = value;
        OnPropertyChanged("InputText");
      }
    }

    /// <summary>
    /// Gets the command for the "OK" button
    /// </summary>
    public ICommand OkCommand { get; private set; }

    /// <summary>
    /// Gets or sets whether the dialog can return an empty value.
    /// </summary>
    public bool AllowEmptyInput
    {
      get
      {
        return this.allowEmptyInput;
      }
      set
      {
        if (this.allowEmptyInput == value)
          return;

        this.allowEmptyInput = value;
        OnPropertyChanged("AllowEmptyInput");
      }
    }

    #endregion

    private void ExecuteOkCommand(object parameter)
    {
      Close(true);
    }

    private bool CanExecuteOkCommand(object parameter)
    {
      if (string.IsNullOrEmpty(this.inputText))
      {
        return this.allowEmptyInput;
      }
      else
        return true;
    }
  }
}
